package com.xujf.sym.ui.activity

import android.content.Intent
import android.os.Bundle
import android.support.v4.view.GravityCompat
import android.support.v4.widget.DrawerLayout
import android.support.v7.widget.Toolbar
import android.text.TextUtils
import android.view.Menu
import android.view.View
import com.bumptech.glide.Glide
import com.xujf.sym.R
import com.xujf.sym.model.storage.LoginShared
import com.xujf.sym.ui.base.BaseActivity
import com.xujf.sym.ui.view.IMainView
import kotlinx.android.synthetic.main.activity_main_center.*
import kotlinx.android.synthetic.main.activity_main_home.*
import kotlinx.android.synthetic.main.activity_main_nav.*

class MainActivity : BaseActivity(), IMainView {

    companion object {
        var isDrawerOpen = false
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_home)
        initView()
        initClick()
        updateUserInfoViews()
    }

    override fun initSlidable() {
        //不侧滑退出
    }

    private fun initView() {
        setSupportActionBar(toolbar)
        toolbar.setNavigationOnClickListener {
            drawer_layout.openDrawer(GravityCompat.START)
        }
//        toolbar.setOnClickListener(DoubleClickBackToContentTopListener(this))
        toolbar.setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener { item ->
            if (item.itemId == R.id.action_search) {
                //TODO
            } else if (item.itemId == R.id.action_message) {
                //TODO
            }
            true
        })

        drawer_layout.addDrawerListener(drawerListener)
    }

    private fun initClick() {
        layout_info.setOnClickListener {
            if (TextUtils.isEmpty(LoginShared.getToken(this))) {
                LoginActivity.startForResult(this)
            } else {
//                UserDetailActivity.startWithTransitionAnimation(this, LoginShared.getLoginName(this), iv_avatar, LoginShared.getAvatarUrl210(this))
            }
        }
    }

    /**
     * 判断登录是否成功
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == LoginActivity.REQUEST_LOGIN && resultCode == RESULT_OK) {
//            mainPresenter.getUserAsyncTask()
            updateUserInfoViews()
        }
    }

    override fun onRefreshTopicListFinish() {

    }

    override fun onLoadMoreTopicListFinish(state: Int) {

    }

    override fun updateUserInfoViews() {
        if (TextUtils.isEmpty(LoginShared.getToken(this))) {
            Glide.with(this)
                    .load(R.drawable.image_placeholder)
                    .placeholder(R.drawable.image_placeholder)
                    .dontAnimate()
                    .into(iv_avatar)
            tv_login_name.setText(R.string.click_avatar_to_login)
            tv_score.text = (null)
            btn_logout.visibility = View.GONE
        } else {
            Glide.with(this)
                    .load(LoginShared.getAvatarUrl210(this))
                    .error(R.drawable.image_placeholder)
                    .placeholder(R.drawable.image_placeholder)
                    .dontAnimate()
                    .into(iv_avatar)
            tv_login_name.text = LoginShared.getLoginName(this)
            tv_score.text = getString(R.string.score, LoginShared.getPoint(this))
            btn_logout.visibility = View.VISIBLE
        }
    }

    override fun updateMessageCountViews(count: Int) {

    }

    override fun logoutUserOk() {

    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    private val drawerListener = object : DrawerLayout.SimpleDrawerListener() {

        override fun onDrawerOpened(drawerView: View?) {
            isDrawerOpen = true
//            updateUserInfoViews()
//            mainPresenter.getDomains()
//            mainPresenter.getMessageCountAsyncTask();
        }

        override fun onDrawerClosed(drawerView: View?) {
            isDrawerOpen = false
//            val tab = TabType.all
//            mainPresenter.switchTab(tab)
        }

    }

}
